<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/7/28
 * Time: 10:04
 */

namespace app\commi\controller;

use think\Db;
use think\Request;

class Rowingmember extends Base
{
    /**
     * 赛艇参赛人员列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        $match_id = Db::table('up_match')
            ->where('match_committee_id', session('com_id'))
            ->where('match_member_id', session('cm_id'))
            ->value('match_id');
        if(request()->isAjax ()){
            $apply_start_time = input('apply_start_time');
            $apply_end_time = input('apply_end_time');
            $ma_sgin_status = input('ma_sgin_status');
            $ma_baodao_status = input('ma_baodao_status');
            $club_id = input('club_id');
            
            $where['ma_match_id'] = $match_id;
            $where['ma_role'] = 1;
            // 报名时间
            if(isset($apply_start_time) && $apply_start_time != '' && isset($apply_end_time) && $apply_end_time != ''){
                $where['ma_time'] = ['between', [$apply_start_time, $apply_end_time]];
            }
            if(isset($apply_start_time) && $apply_start_time != '' && isset($apply_end_time) && $apply_end_time == ''){
                $where['ma_time'] = ['>=', $apply_start_time];
            }
            if(isset($apply_start_time) && $apply_start_time == '' && isset($apply_end_time) && $apply_end_time != ''){
                $where['ma_time'] = ['<=', $apply_end_time];
            }
            // 签到状态
            if(isset($ma_sgin_status) && $ma_sgin_status != ''){
                $where['ma_sgin_status'] = $ma_sgin_status;
            }
            // 报到状态
            if(isset($ma_baodao_status) && $ma_baodao_status != ''){
                $where['ma_baodao_status'] = $ma_baodao_status;
            }
            // 俱乐部
            if(isset($club_id) && $club_id != ''){
                $where['club_id'] = $club_id;
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('match_apply_info')
                ->join('up_club', 'ma_club_id = club_id', 'left')
                ->join('up_match_group', 'mg_id = ma_group', 'left')
                ->join('up_match', 'ma_match_id = match_id', 'left')
                ->where($where)
//                ->group('ma_team_name')
                ->count();// 获取总条数
            $lists = Db::name('match_apply_info')
                ->join('up_club', 'ma_club_id = club_id', 'left')
                ->join('up_match_group', 'mg_id = ma_group', 'left')
                ->join('up_match', 'ma_match_id = match_id', 'left')
                ->field('ma_id, match_status, club_name, mg_name, ma_team_name, ma_distance, ma_boat_type, 
                ma_total_price, ma_date, ma_sgin_status, ma_baodao_status, ma_lingd')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('ma_time desc')
//                ->group('ma_team_name')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $club = Db::table('up_match_apply_info')
            ->join('up_club', 'club_id = ma_club_id', 'left')
            ->field('club_id, club_name')
            ->where('ma_match_id', $match_id)
            ->group('ma_club_id')
            ->select();
        $this->assign([
            'club' => $club
        ]);
        return $this->fetch();
    }
    
    /**
     * 编辑报名
     * @return mixed|\think\response\Json
     */
    public function applyEdit(){
        $ma_id = input('ma_id');
        $apply = Db::table('up_match_apply_info')
            ->where('ma_id', $ma_id)
            ->field('ma_team_name, ma_boat_type, ma_distance, ma_match_id, ma_group')
            ->find();
        if(Request::instance()->isPost()){
            $ma_team_name = trim(input('ma_team_name'), ' ');
            $ma_group = input('ma_group');
            $ma_boat_type = input('ma_boat_type');
            $ma_distance = input('ma_distance');
            
            // 组别
            if($apply['ma_group'] != $ma_group){
                $group_num = Db::table('up_match_apply_group')
                    ->where('mag_match_id', $apply['ma_match_id'])
                    ->where('mag_group_id', $ma_group)
                    ->field('mag_apply_num, mag_already_num')
                    ->find();
                if($group_num['mag_already_num'] >= $group_num['mag_apply_num']){
                    return json(['code' => 100, 'msg' => '该组别报名人数已满']);
                }
            }
            // 队名
            $team_ma_id = Db::table('up_match_apply_info')
                ->where('ma_team_name', $apply['ma_team_name'])
                ->where('ma_match_id', $apply['ma_match_id'])
                ->column('ma_id');
            $same_team = Db::table('up_match_apply_info')
                ->where('ma_match_id', $apply['ma_match_id'])
                ->where('ma_team_name', $ma_team_name)
                ->where('ma_id', 'NOTIN', $team_ma_id)
                ->find();
            if($same_team){
                return json(['code' => 100, 'msg' => '该队名已被使用']);
            }
            
            $update = [
                'ma_team_name' => $ma_team_name,
                'ma_group' => $ma_group,
                'ma_boat_type' => $ma_boat_type,
                'ma_distance' => $ma_distance
            ];
            
            Db::startTrans();
            try{
                Db::table('up_match_apply_info')->where('ma_id', 'IN', $team_ma_id)->update($update);
                if($apply['ma_group'] != $ma_group){
                    // 原组别报名人数减一
                    Db::table('up_match_apply_group')
                        ->where('mag_match_id', $apply['ma_match_id'])
                        ->where('mag_group_id', $apply['ma_group'])
                        ->setDec('mag_already_num');
                    // 现组别报名人数加一
                    Db::table('up_match_apply_group')
                        ->where('mag_match_id', $apply['ma_match_id'])
                        ->where('mag_group_id', $ma_group)
                        ->setInc('mag_already_num');
                }
                Db::commit();
                return json(['code' => 200, 'msg' => '编辑成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $group = Db::table('up_match_apply_group')
            ->join('up_match_group', 'mg_id = mag_group_id', 'left')
            ->where('mag_match_id', $apply['ma_match_id'])
            ->field('mag_group_id, mg_name')
            ->select();
        $this->assign([
            'apply' => $apply,
            'group' => $group,
            'ma_id' => $ma_id
        ]);
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            return $this->fetch('rowingmember/applyedit');
        }
    }
    
    /**
     * 船员列表
     * @return mixed|\think\response\Json
     */
    public function member(){
        if(request()->isAjax ()){
            $user_keyword = trim(input('user_keyword'), ' ');
            $info = Db::table('up_match_apply_info')
                ->where('ma_id', session('ma_id'))
                ->field('ma_match_id, ma_team_name')
                ->find();
        
            $where['ma_team_name'] = $info['ma_team_name'];
            $where['ma_match_id'] = $info['ma_match_id'];
        
            // 注册手机号/ID/队员名称
            if(isset($user_keyword) && $user_keyword != ''){
                $where['user_phone|user_create_id|ma_user_name'] = ['like', '%' . $user_keyword . '%'];
            }
        
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('match_apply_info')
                ->join('up_u_user', 'ma_user_id = user_id', 'left')
                ->join('up_match', 'ma_match_id = match_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('match_apply_info')
                ->join('up_u_user', 'ma_user_id = user_id', 'left')
                ->join('up_match', 'ma_match_id = match_id', 'left')
                ->field('ma_id, ma_user_name, user_phone, user_create_id, ma_user_gender, ma_user_email, 
                ma_user_call, match_status, ma_user_urg_phone, ma_user_ID_number, ma_role, ma_seat')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('ma_role asc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $ma_id = input('ma_id');
        if(isset($ma_id) && $ma_id != ''){
            session('ma_id', $ma_id);
        }
        $match_status = Db::table('up_match_apply_info')
            ->join('up_match', 'match_id = ma_match_id', 'left')
            ->where('ma_id', session('ma_id'))
            ->value('match_status');
        $this->assign([
            'match_status' => $match_status
        ]);
        return $this->fetch('rowingmember/member');
    }
    
    /**
     * 添加船员
     * @return mixed|\think\response\Json
     */
    public function memberAdd(){
        if(Request::instance()->isPost()){
            $user_id = input('user_id');
            $user_info = Db::table('up_u_user')
                ->field('user_club_id, user_real_check')
                ->where('user_id', $user_id)
                ->find();
            if($user_info['user_real_check'] == 1){
                return json(['code' => 100, 'msg' => '该用户尚未实名认证']);
            }
            $info = Db::table('up_match_apply_info')
                ->where('ma_id', session('ma_id'))
                ->field('ma_match_id, ma_total_price, ma_cost_info_id, ma_club_id, ma_lingd, ma_group, 
                ma_team_name, ma_boat_type, ma_distance, ma_real_pay_user_id, ma_order_num, ma_time, 
                ma_date, ma_apply_status, ma_match_cate')
                ->find();
            $user = Db::table('up_u_user')
                ->where('user_id', $user_id)
                ->field('user_phone, user_real_name, user_gender, user_birthday, user_ID_number, user_call, 
            user_email, user_urg_phone, user_sailing_time')
                ->find();
            
            $match = Db::table('up_match')
                ->field('match_start_time')
                ->where('match_id', $info['ma_match_id'])
                ->find();
            // 已报名本赛事
            $thisMatch = Db::table('up_match_apply_info')
                ->where('ma_user_id', $user_id)
                ->where('ma_match_id', $info['ma_match_id'])
                ->where('ma_apply_status', 2)
                ->find();
            if($thisMatch){
                return json(['code' => 100, 'msg' => '该用户已报名本场比赛']);
            }
            // 其他比赛
            $otherMatch = Db::table('up_match_apply_info')
                ->join('up_match', 'match_id = ma_match_id', 'left')
                ->where('ma_user_id', $user_id)
                ->where('match_end_time', '>', $match['match_start_time'])
                ->where('match_id', 'NEQ', $info['ma_match_id'])
                ->where('ma_apply_status', 2)
                ->field('match_id')
                ->find();
            if($otherMatch) return json(['status' => 0, 'msg' => '该用户已报名其他有冲突的赛事']);
    
            $insert = [
                'ma_user_id' => $user_id,
                'ma_match_id' => $info['ma_match_id'],
                'ma_total_price' => $info['ma_total_price'],
                'ma_cost_info_id' => $info['ma_cost_info_id'],
                'ma_club_id' => $info['ma_club_id'],
                'ma_lingd' => $info['ma_lingd'],
                'ma_group' => $info['ma_group'],
                'ma_team_name' => $info['ma_team_name'],
                'ma_boat_type' => $info['ma_boat_type'],
                'ma_distance' => $info['ma_distance'],
                'ma_role' => 2,
                'ma_real_pay_user_id' => $info['ma_real_pay_user_id'],
                'ma_order_num' => $info['ma_order_num'],
                'ma_time' => $info['ma_time'],
                'ma_date' => $info['ma_date'],
                'ma_match_cate' => $info['ma_match_cate'],
                'ma_apply_status' => $info['ma_apply_status'],
                'ma_user_name' => $user['user_real_name'],
                'ma_user_gender' => $user['user_gender'],
                'ma_user_birthday' => $user['user_birthday'],
                'ma_user_ID_number' => $user['user_ID_number'],
                'ma_user_call' => $user['user_call'],
                'ma_user_email' => $user['user_email'],
                'ma_user_urg_phone' => $user['user_urg_phone'],
                'ma_user_sailing_time' => $user['user_sailing_time']
            ];
            $res = Db::table('up_match_apply_info')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $user = Db::table('up_u_user')
            ->where('user_status', 1)
            ->field('user_id, user_real_name, user_phone')
            ->select();
        $this->assign([
            'user' => $user
        ]);
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            return $this->fetch('rowingmember/memberadd');
        }
    }
    
    /**
     * 队员详情
     * @return mixed
     */
    public function memberDetail(){
        $ma_id = input('ma_id');
        $detail = Db::table('up_match_apply_info')
            ->join('up_match', 'match_id = ma_match_id', 'left')
            ->join('up_u_user uuu1', 'uuu1.user_id = ma_user_id', 'left')
            ->join('up_club', 'ma_club_id = club_id', 'left')
            ->join('up_match_group', 'ma_group = mg_id', 'left')
            ->join('up_u_user uuu2', 'ma_hexiao_user_id = uuu2.user_id', 'left')
            ->where('ma_id', $ma_id)
            ->field('ma_user_name, ma_user_gender, ma_user_ID_number, ma_user_email, ma_user_call, 
            ma_user_urg_phone, club_name, ma_lingd, ma_user_sailing_time, ma_match_id, match_title, 
            ma_total_price, ma_cost_info_id, mg_name, ma_baodao_time, uuu2.user_username hexiao_name, 
            ma_sgin_time, ma_sgin_addr, ma_seat, ma_role, uuu1.user_height, uuu1.user_weight, uuu1.user_fuzhuang')
            ->find();
        $info = Db::table('up_match_cost_info')
            ->join('up_match_cost', 'mc_id = mi_cost_id', 'left')
            ->where('mi_id', 'IN', $detail['ma_cost_info_id'])
            ->field('mc_name, mi_price')
            ->select();
        $detail['cost_info'] = $info;
        $this->assign([
            'detail' => $detail
        ]);
        return $this->fetch('rowingmember/memberdetail');
    }
    
    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function memberEdit(){
        $ma_id = input('ma_id');
        if(Request::instance()->isPost()){
            $user_real_name = trim(input('user_real_name'), ' ');
            $user_gender = input('user_gender');
            $user_birthday = input('user_birthday');
            $user_ID_number = trim(input('user_ID_number'), ' ');
            $user_call = trim(input('user_call'), ' ');
            $user_email = trim(input('user_email'), ' ');
            $user_urg_phone = trim(input('user_urg_phone'), ' ');
            $user_sailing_time = trim(input('user_sailing_time'), ' ');
            $user_id = input('user_id');
            $ma_seat = input('ma_seat');
            $update = [
                'ma_user_name' => $user_real_name,
                'ma_user_gender' => $user_gender,
                'ma_user_birthday' => $user_birthday,
                'ma_user_ID_number' => $user_ID_number,
                'ma_user_call' => $user_call,
                'ma_user_email' => $user_email,
                'ma_user_urg_phone' => $user_urg_phone,
                'ma_user_sailing_time' => $user_sailing_time,
                'ma_seat' => $ma_seat,
                'ma_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_match_apply_info')->where('ma_id', $ma_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $user = Db::table('up_match_apply_info')
            ->join('up_u_user', 'user_id = ma_user_id', 'left')
            ->where('ma_id', $ma_id)
            ->field('user_phone, user_real_name, user_gender, user_birthday, user_ID_number, user_call, 
            user_email, user_urg_phone, user_sailing_time, user_id, ma_seat')
            ->find();
        $this->assign([
            'user' => $user,
            'ma_id' => $ma_id
        ]);
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            return $this->fetch('rowingmember/memberedit');
        }
    }
    
    /**
     * 删除船员
     * @return \think\response\Json
     */
    public function memberDel(){
        $lose = $this->lostTime();
        if($lose == 1){
            return json(['code' => 100, 'msg' => '账号已过期，非法操作']);
        }
        $ma_id = input('id');
        $res = Db::table('up_match_apply_info')->delete($ma_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}